package ar.com.jwa.domain;

import java.util.Date;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import ar.com.jwa.commons.architecture.PersistentObject;

@Entity
@Table(name="Games")
public class Game extends PersistentObject {
	
	@Transient
	private Date theDate;
	
	@Transient
	private String result;
	
	private Set<User> players = new HashSet<User>();
	
	@Transient
	private State state;
	
	public Game(Date theDate) {
		super();
		this.theDate = theDate;
		this.state = State.TO_CONFIRM;
	}
	
	public Game() {
		super();
	}

	/**
	 * @return the theDate
	 */
	@Column(name="date")
	public Date getTheDate() {
		return theDate;
	}

	/**
	 * @param theDate the theDate to set
	 */
	public void setTheDate(Date theDate) {
		this.theDate = theDate;
	}
	
	
	/**
	 * @return the result
	 */
	@Column(name="result")
	public String getResult() {
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}

	/**
	 * @return the state
	 */
	public State getState() {
		return state;
	}

	/**
	 * @param state the state to set
	 */
	public void setState(State state) {
		this.state = state;
	}
	
	/**
	 * @return the players
	 */
	
	@ManyToMany(cascade = CascadeType.ALL, fetch = javax.persistence.FetchType.LAZY, targetEntity = User.class)
	@JoinTable(name = "game_user", joinColumns = { @JoinColumn(name = "gameId") }, inverseJoinColumns = { @JoinColumn(name = "userId") })
	public Set<User> getPlayers() {
		return players;
	}

	public void addPlayer(User player){
		this.getPlayers().add(player);
	}
	
	public boolean hasPlayer(User player){
		return this.getPlayers().contains(player);
	}
	
	/**
	 * @param players the players to set
	 */
	public void setPlayers(Set<User> players) {
		this.players = players;
	}
}
